//
//  HMComposeButton.swift
//  WeiBo14
//
//  Created by HM on 16/3/9.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit

class HMComposeButton: UIButton {
    
    // 当前按钮显示的数据
    var menuInfo: HMComposeMenuInfo? {
        didSet{
            setImage(UIImage(named: menuInfo!.icon!), forState: UIControlState.Normal)
            setTitle(menuInfo!.title!, forState: UIControlState.Normal)
        }
    }
    
    // Swift 去掉按钮的高亮效果
    override var highlighted: Bool {
        get{
            return false
        }
        set{
            
        }
    }

    // 如果去掉高亮效果
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        // 文字居中
        titleLabel?.textAlignment = .Center
        // 文字颜色
        setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        // 文字大小
        titleLabel?.font = UIFont.systemFontOfSize(16)
        // 让图片原样显示
        imageView?.contentMode = .Center
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let width = self.frame.width
        let height = self.frame.height
        
        // 1. 调整图片的大小以及位置
        imageView?.frame.size = CGSize(width: width, height: width)
        imageView?.frame.origin = CGPointZero
        
        // 2. 调整label的大小以及位置
        titleLabel?.frame.size = CGSize(width: width, height: height - width)
        titleLabel?.frame.origin = CGPoint(x: 0, y: width)
    }
}
